﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AspNetTraining.BLL
{
    public class UserAccountManager
    {
        /// <summary>
        /// Check account
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public UserAccount GetUserByUserNamePassword(string username = "", string password = "")
        {
            UserAccount result = null;

            using (var context = new DotNetTrainingEntities())
            {
                result = context.UserAccounts.Where(c => c.UserName == username && c.Password == password && c.Active == true).FirstOrDefault();
            }
            return result;
        }
        /// <summary>
        /// List all Account
        /// </summary>
        /// <returns></returns>
        public List<UserAccount> GetListAccount()
        {
            using (var context = new DotNetTrainingEntities())
            {
                return (context.UserAccounts).ToList<UserAccount>();
            }
        }
        /// <summary>
        /// Get account by ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserAccount GetAccountByID(System.Guid id)
        {
            using (var context = new DotNetTrainingEntities())
            {
                return (AspNetTraining.BLL.UserAccount)(context.UserAccounts).Where(u => u.UserAccountID == id).FirstOrDefault();
            }
        }
        /// <summary>
        /// Get account Name
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public String GetUserNameByUserID(System.Guid ID)
        {
            using (var context = new DotNetTrainingEntities())
            {
                return (context.UserAccounts).Where(u => u.UserAccountID == ID).FirstOrDefault().UserName.ToString();
            }
        }
        /// <summary>
        /// Update Account
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool UpDateUserAccount(UserAccount user)
        {
            using (var context = new DotNetTrainingEntities())
            {
                context.Entry(user).State = EntityState.Modified;
                context.SaveChanges();
            }
            return true;
        }
        /// <summary>
        /// Insert account to sql
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool InsertUserAccount(UserAccount user)
        {
            using (var context = new DotNetTrainingEntities())
            {
                context.UserAccounts.Add(user);
                try
                {
                    context.SaveChanges();
                    return true;
                }
                catch { return false; }

            }

        }
        /// <summary>
        /// kiem tra tai khoan nay da ton tai hay chua
        /// true= chua ton tai - nguoc lai
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public bool CheckUserAccount(string username)
        {
            using (var context = new DotNetTrainingEntities())
            {
                try {
                    return (context.UserAccounts).Where(u => u.UserName == username).FirstOrDefault() == null ? true : false;                    
                }
                catch { return true; }
            }
        }
        /// <summary>
        /// delete acc by Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteAccount(System.Guid id)
        {
            using (var context = new DotNetTrainingEntities())
            {
                try
                {
                   UserAccount user= context.UserAccounts.Where(scc => scc.UserAccountID == id).FirstOrDefault();
                   //kiem tra nhom he thong
                   if (user.Builtin == true)
                       return false;
                   context.Entry(user).State = EntityState.Deleted;
                   context.SaveChanges();
                   return true;
                }
                catch { return false; }
            }
        }
        /// <summary>
        /// delete user by username
        /// </summary>
        /// <param name="acc"></param>
        /// <returns></returns>
        public bool DeleteAccountByUserName(string acc)
        {
            using (var context = new DotNetTrainingEntities())
            {
                try
                {
                    UserAccount user = context.UserAccounts.Where(scc => scc.UserName == acc).FirstOrDefault();
                    //kiem tra nhom he thong
                    if (user.Builtin == true)
                        return false;
                    context.Entry(user).State = EntityState.Deleted;
                    context.SaveChanges();
                    return true;
                }
                catch { return false; }

            }
        }
        /// <summary>
        /// kiem tra xem ton tai thanh vien trong nhom hay ko.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool CheckUserGroup(string id)
        {
            using (var context = new DotNetTrainingEntities())
            {
                try
                {
                    return (context.UserAccounts).Where(u => u.GroupUserID == id).FirstOrDefault() == null ? true : false;
                }
                catch { return true; }
            }
        }
    }
}
